<?php
declare (strict_types=1);

namespace app\api\controller\v2;

use app\api\validate\LoginValidate;
use app\common\model\ApiMember;
use app\common\model\UserToken;
use think\cache\driver\Redis;
use think\captcha\facade\Captcha;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Session;

class Site
{
    /**
     * 登录
     * account：账号
     * password：密码
     */
    public function login()
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(LoginValidate::class)->scene('login')->check($postData);
            $redis = new Redis();
            $cache = $redis->get(strtolower($postData['code']));
            if($cache == null){
                return errMsg(400,'验证码错误或失效');
            }
            $del = $redis->delete(strtolower($postData['code']));
            $user = new ApiMember();
            // 检测用户是否存在
            $memberInfo = $user->fetchInfoByWhere(['account' => $postData['account']], 'id,account,salt,password_hash');
            if (empty($memberInfo)) {
                return errMsg(422, '账号不存在！');
            }
            // 验证密码
            if (!checkPassword($postData['password'], $memberInfo)) {
                return errMsg(422, '密码错误！');
            }
            // 查询token，检测是否有效
            $userToken = UserToken::fetchToken($memberInfo->id);
            if (empty($userToken)) {
                $userToken = signToken($memberInfo->id, 'api');
                $tokenModel = new UserToken();
                if (!$tokenModel->createToken($memberInfo->id, $userToken)) {
                    return errMsg(500, '系统错误，请联系管理员！');
                }
            } else {
                $resToken = checkToken($userToken);
                if ($resToken['code'] == -1) {
                    $tokenModel = new UserToken();
                    $userToken = signToken($memberInfo->id, 'api');
                    if (!$tokenModel->createToken($memberInfo->id, $userToken)) {
                        return errMsg(500, '系统错误，请联系管理员！');
                    }
                }
            }
            // 返回用户数据
            $resMember = [
                'id' => $memberInfo->id,
                'account' => $memberInfo->account,
                'token' => $userToken
            ];
            $login_times = time();
            $login = $user ->update(['login_times'=>$login_times,'id'=>$resMember['id']]);
            return sucessMsg(200, '登录成功', $resMember);
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 注册
     * account：账号
     * password：mima
     */
    public function register()
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(LoginValidate::class)->scene('register')->check($postData);
            $user = new ApiMember();
            $memberInfo = $user->fetchInfoByWhere(['account' => $postData['account']], 'id,account');
            if (!empty($memberInfo)) {
                return errMsg(422, '账号已存在！');
            }
            $phone = $user->where('phone',$postData['phone'])->find();
            if (!empty($phone)) {
                return errMsg(422, '该手机号已注册！');
            }
            if ($postData['password'] !== $postData['sure_password']) {
                return errMsg(422, '两次输入密码不一致！');
            }
            $postData['salt'] = getRandStr(6);
            $postData['password_hash'] = makePassword($postData['password'], $postData['salt']);
            $postData['image'] = "uploads/20210629/07d23ff2edd3a8ef3697e5987a7ec31d.png";
            if ($user->save($postData)) {
                return sucessMsg(200, '注册成功');
            }
            return errMsg(422, '注册失败，请重试！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }
}
